import { CronJob } from 'cron';
import MuseumTask from './museumTask';
import MuseumTimesTask from './museumTimesTask';
import PalaceTask from './palaceTask';
import PlaceUserStatusTask from './placeUserStatusTask';

const mUserTask = async () => {
  const museumTask = new MuseumTask();
  await museumTask.init();
  museumTask.execute();
};

const mUserTimesTask = async () => {
  const museumTask = new MuseumTimesTask();
  museumTask.execute();
};

const pUserTask = async () => {
  // 先检查用户
  const userTask = new PlaceUserStatusTask();
  userTask.execute();
  const task = new PalaceTask();
  await task.init();
  checkTime(task);
};

const checkTime = (task) => {
  setTimeout(() => {
    const date = new Date();
    // 20:00 开始放票
    if(date.getHours() === 20){
      task.execute();
    } else {
      // 继续检查
      checkTime(task);
    }
  }, 100);
}


new CronJob('*/10 * * * * *', mUserTask).start();
new CronJob('1 17 * * *', mUserTimesTask).start();

new CronJob('00 55 19 * * *', pUserTask).start();